<template>
  <div class="app-container">
    <!--搜索栏-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="关键字搜索" label-width="85" prop="keyValue">
        <el-input
          v-model="queryParams.keyValue"
          placeholder="请输入搜索关键字"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态:" label-width="140px">
        <el-select v-model="queryParams.dealStatus" placeholder="请选择处理状态">
          <el-option
            v-for="dict in dealStatusOption"
            :key="dict.id"
            :label="dict.label"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="SOS时间" label-width="140px">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--数据列表-->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column
        label="告警用户"
        prop="userName"
        width="150"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="用户头像" prop="faceUrl" />
      <el-table-column label="求救时间" prop="helpTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.helpTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理意见" prop="notes">
        <template #default="scope">
          <span>{{ scope.row.notes ? scope.row.notes : "---" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" prop="dealStatus">
        <template #default="scope">
          <el-switch
            v-model="scope.row.dealStatus"
            inline-prompt
            active-value="1"
            inactive-value="5"
            active-text="已处理"
            inactive-text="未处理"
            disabled
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip
            v-if="scope.row.dealStatus == 5 || scope.row.dealStatus == '5'"
            content="处理"
            placement="top"
          >
            <el-button link type="primary" @click="handleResult(scope.row)"
              >处理</el-button
            >
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />

    <!--设置SOS告警处理意见对话框-->
    <el-dialog :title="title" v-model="open" width="60%" append-to-body>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="处理意见:" label-width="140px" prop="notes">
          <el-input
            type="textarea"
            :rows="3"
            v-model="form.notes"
            placeholder="请输入处理意见"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="SOS">
import { listSos, exportSos, handleSos } from "@/api/screen/sos";
const total = ref(0);
const open = ref(false);
const title = ref("");
const loading = ref(false);
const dataList = ref([]); //数据列表
const dateRange = ref([]);
const dealStatusOption = ref([
  { id: "1", label: "已处理" },
  { id: "5", label: "未处理" },
]);
const data = reactive({
  form: {},
  queryParams: {
    current: 1,
    size: 10,
    keyValue: undefined,
  },
  rules: {},
});
const { queryParams, form, rules } = toRefs(data);
/** 获取SOS告警数据 */
function getList() {
  loading.value = true;
  if (dateRange.value.length > 0) {
    queryParams.value.beginTime = dateRange.value[0];
    queryParams.value.endTime = dateRange.value[1];
  }
  listSos(queryParams.value).then((response) => {
    loading.value = false;
    dataList.value = response.data.dataList;
    total.value = response.data.totalNum;
  });
}

/** 处理查询 */
function handleQuery() {
  getList();
}
/** 重置查询 */
function resetQuery() {
  dateRange.value = [];
  queryParams.value = {
    current: 1,
    size: 10,
  };
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 处理告警 */
function handleResult(row) {
  form.value = row;
  open.value = true;
  title.value = "处理SOS告警";
}

/** 取消 */
function cancel() {
  reset();
  open.value = false;
}

/** 处理意见提交 */
function submitForm() {
  if (form.value.id != undefined && form.value.notes != null) {
    form.value.dealStatus = "1";
    handleSos(form.value).then((response) => {
      open.value = false;
      getList();
    });
  } else {
    proxy.$modal.msgError("请填写处理意见");
  }
}
getList();
</script>
